package com.android.cookbook.activity;

import java.io.IOException;
import java.util.ArrayList;

import com.android.cookbook.R;
import com.android.cookbook.R.id;
import com.android.cookbook.R.layout;
import com.android.cookbook.adapter.RecipeAdapter;
import com.android.cookbook.bean.Recipe;
import com.android.cookbook.db.BaseDatosHelper;
import com.android.cookbook.util.Constant;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;   
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.TextView;

public class ListDriver extends ListActivity {     
	
	BaseDatosHelper myDBHelper;
	ArrayList<Recipe> recipes;
	String strTypeId = "";
	String strSearchType = "";
	String strTypeName = "";

	/** Called when the activity is first created. */    
	@Override    
	public void onCreate(Bundle savedInstanceState) {         
		super.onCreate(savedInstanceState);         
		setContentView(R.layout.list); //establecemos el Layout de esta Actividad

		TextView title = (TextView) findViewById(R.id.title_list);
		
		createDB();

		Bundle extras = getIntent().getExtras();         
		if (extras != null) {             
			strTypeId = extras.getString(Constant.TYPE_ID);
			strTypeName = extras.getString(Constant.TYPE_NAME);
			strSearchType = extras.getString(Constant.TYPE_SEARCH);
			
			title.setText(strTypeName);
			
			if (Constant.LIST_BY_ID.equals(strSearchType)){ // list by id
				recipes = getItems(Integer.parseInt(strTypeId));         
				setListAdapter(new RecipeAdapter(this, R.layout.item_list, recipes));
			}else{ // list by name
				recipes = getItems(strTypeId);         
				setListAdapter(new RecipeAdapter(this, R.layout.item_list, recipes));
			}
		}
		
        ImageView home = (ImageView) findViewById(R.id.home);
        
        home.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {                   
        		Intent myIntentCreate = new Intent(view.getContext(),CookBookActivity.class);
            	startActivityForResult(myIntentCreate, 0);             
        	}          
        });
        
        ImageView create = (ImageView) findViewById(R.id.create_list);
        
        create.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {                   
        		Intent myIntentCreate = new Intent(view.getContext(),RecipeCreate.class);
            	startActivityForResult(myIntentCreate, 0);             
        	}          
        });
        
        ImageView search = (ImageView)findViewById(R.id.search_list);
        
        search.setOnClickListener(new View.OnClickListener() {             
        	public void onClick(View view) {                   
            	Intent myIntent = new Intent(view.getContext(),RecipeSearch.class);
            	startActivityForResult(myIntent, 0);            
        	}          
        });
		
        ListView list = getListView();
        registerForContextMenu(list); 
	}

	@Override    
	public void onRestart(){
		super.onRestart();
		TextView title = (TextView) findViewById(R.id.title_list);
		Bundle extras = getIntent().getExtras();         
		if (extras != null) {             
			strTypeId = extras.getString(Constant.TYPE_ID);
			strTypeName = extras.getString(Constant.TYPE_NAME);
			strSearchType = extras.getString(Constant.TYPE_SEARCH);
			
			title.setText(strTypeName);
			
			if (Constant.LIST_BY_ID.equals(strSearchType)){ // list by id
				recipes = getItems(Integer.parseInt(strTypeId));         
				setListAdapter(new RecipeAdapter(this, R.layout.item_list, recipes));
			}else{ // list by name
				recipes = getItems(strTypeId);         
				setListAdapter(new RecipeAdapter(this, R.layout.item_list, recipes));
			}
		}
	}
	
	public void createDB() {         
		myDBHelper = new BaseDatosHelper(this);         
		try {             
			myDBHelper.createDataBase();         
		} catch (IOException ioe) {             
			throw new Error("Unable to create database");         
		}     
	}
	
	public ArrayList<Recipe> getItems() {         
		myDBHelper.openDataBase();         
		ArrayList<Recipe> recipeList = myDBHelper.getRecipeList();         
		myDBHelper.close();         
		return recipeList;     
	}
	
	public ArrayList<Recipe> getItems(int type_id) {         
		myDBHelper.openDataBase();         
		ArrayList<Recipe> recipeList = myDBHelper.getRecipeList(type_id);         
		myDBHelper.close();         
		return recipeList;     
	}

	public ArrayList<Recipe> getItems(String item_search) {         
		myDBHelper.openDataBase();         
		ArrayList<Recipe> recipeList = myDBHelper.getRecipeList(item_search);         
		myDBHelper.close();         
		return recipeList;     
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add(0, Constant.DELETE, 0, "Eliminar");
		menu.add(0, Constant.EDIT, 0, "Editar");
	}

	public boolean onContextItemSelected (MenuItem item) {
		myDBHelper.openDataBase(); 
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		Recipe recipe = recipes.get(info.position);

		switch (item.getItemId()){
			case Constant.DELETE:
				myDBHelper.deleteRecipe(recipe.get_id(), recipe.getRectypeid());
				ArrayList <Recipe> recipes_result = getItems(Integer.parseInt(strTypeId));         
				setListAdapter(new RecipeAdapter(this, R.layout.item_list, recipes_result));
				return true;
			case Constant.EDIT:
        		Bundle bundle = new Bundle();
        		bundle.putString(Constant.TYPE_CREATE,"EDIT");
        		bundle.putInt("_id",recipe.get_id());
        		Intent myIntentCreate = new Intent(this,RecipeCreate.class);
        		myIntentCreate.putExtras(bundle); 
            	startActivityForResult(myIntentCreate, 0);
			default:
				return super.onContextItemSelected(item);
		}
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Bundle bundle = new Bundle();
		Recipe recipe = recipes.get(position); 
		String id_detail = String.valueOf(recipe.get_id());
		bundle.putString(Constant.TYPE_ID,id_detail);
		Intent myIntent = new Intent(this,DetailDriver.class);
		myIntent.putExtras(bundle);                   
		startActivityForResult(myIntent,0); 
	}
}
